Skip to content

Fix nested dataclass check in StaticDataclass - #2939

Open
Nas01010101 wants to merge 1 commit into
google-deepmind:mainfrom
Nas01010101:fix-static-dataclass-nested-check
Open

Nas01010101 wants to merge 1 commit into
google-deepmind:mainfrom
Nas01010101:fix-static-dataclass-nested-check

Conversation

@Nas01010101

Copy link
Copy Markdown

Fixes #2938.

StaticDataclass.__post_init__ checks that every field holding a dataclass is itself a StaticDataclass. The loop iterates for v, field in zip(values, fields) but tests cur_value, a variable left over from the hash-by-id loop. That variable always holds the last field's value, so a plain dataclass in any field other than the last is accepted, and one in the last field is reported under the first field's name.

The change tests v instead of cur_value, which also removes the pyrefly: ignore[unbound-name].

The existing test_dataclass_field_is_static passes on main only because NotAStaticDataclass uses eq=False, so it hashes by id and the hash-by-id check raises first. The new test puts a frozen dataclass that hashes by value in the first of two fields. It fails on main and passes with this change.

Testing:

  • pytest torax/_src/tests/static_dataclass_test.py: 20 passed with the change; with the fix reverted, the new test fails with TypeError not raised.
  • pytest -n 3 torax/_src with TORAX_JAXTYPING=True TORAX_ERRORS_ENABLED=True: 3046 passed, 25 skipped, 0 failed.
  • pyink --check on both files: no changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StaticDataclass nested dataclass check only inspects the last field

1 participant